package com.ezhuan.common.util;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.Callable;

import com.ezhuan.system.pojo.ASmsHistory;
import com.ezhuan.system.service.ISystemService;

/**
 * 多线程
 * 
 * @author wangshSxt
 *
 */
public class MultiThread implements Callable<Object>
{
	/* 参数 */
	private Map<String, Object> paramsMap;

	@Override
	public Object call() throws Exception
	{
		/* 往线程存放一个容器 */
		String threadId = UUID.randomUUID() + "" ;
		ConstatFinalUtil.LOGGER.info("---- 线程开始:id:{} ---",threadId);
		
		Date now = new Date();
		Map<String, String> threadMap = new HashMap<String, String>() ; 
		threadMap.put("id", threadId);
		threadMap.put("now", now.toLocaleString());
		threadMap.put("paramsMap", paramsMap + "");
		ConstatFinalUtil.THREAD_MAP.put(threadId, threadMap);
		
		/* 多线程类型 */
		String operType = paramsMap.get("operType") + "";
		if("sendSms".equalsIgnoreCase(operType))
		{
			/* 发送短信 */
			this.sendSmsUtil();
		}
		
		/* 线程执行完成,从线程容器中删除 */
		ConstatFinalUtil.THREAD_MAP.remove(threadId);
		ConstatFinalUtil.LOGGER.info("---- 线程结束:id:{},参数:{} ---",threadId,paramsMap);
		return null;
	}
	
	/**
	 * 发送短信
	 */
	private void sendSmsUtil()
	{
		ConstatFinalUtil.LOGGER.info("---- 发送短信的线程开始 ----");
		ISystemService systemService = (ISystemService) paramsMap.get("systemService");
		ASmsHistory smsHistory = (ASmsHistory) paramsMap.get("smsHistory");
		/* 发送短信 */
		systemService.updateSendSmsService(smsHistory);
		ConstatFinalUtil.LOGGER.info("---- 发送短信的线程结束 ----");
	}

	public Map<String, Object> getParamsMap()
	{
		return paramsMap;
	}

	public void setParamsMap(Map<String, Object> paramsMap)
	{
		this.paramsMap = paramsMap;
	}

}
